package com.service.user.impl;

import com.dao.FootprintDao;
import com.entity.FootprintItem;
import com.service.user.FootprintService;
import com.service.user.GoodService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/** 提供和足迹相关的功能实现 */
@Service
public class FootprintServiceImpl implements FootprintService {
  @Autowired GoodService goodService;

  @Override
  public int getFootprintCount(String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    FootprintDao dao = session.getMapper(FootprintDao.class);
    int count = dao.selectFootprintCount(userId);
    session.close();
    return count;
  }

  /**
   * 获取足迹指定页列表
   *
   * @param userId 用户编号
   * @param pageSize 页大小
   * @param pageIndex 叶索引
   * @return
   */
  @Override
  public List<FootprintItem> getFootprintList(String userId, int pageSize, int pageIndex) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    FootprintDao dao = session.getMapper(FootprintDao.class);
    List<FootprintItem> fpl = dao.selectFootprintList(userId, pageSize * pageIndex, pageSize);
    session.close();
    return fpl;
  }

  /**
   * 删除足迹条目
   *
   * @param userId 用户编号
   * @param visiTime 浏览时间
   * @return 删除成功返回1，否则返回0
   */
  @Override
  public int delFootPrintItem(String userId, String visiTime) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    FootprintDao dao = session.getMapper(FootprintDao.class);
    int row = dao.deleteFootprintItem(userId, visiTime);
    session.commit();
    session.close();
    return row;
  }

  /**
   * 添加足迹
   *
   * @param userId 用户编号
   * @param goodId 商品编号
   * @return 添加成功返回1，否则返回0
   */
  @Override
  public int addFootprintItem(String userId, String goodId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    FootprintDao dao = session.getMapper(FootprintDao.class);
    int row = dao.insertFootprintItem(userId, goodId);
    session.commit();
    session.close();
    return row;
  }
}
